/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.courses;

/**
 *
 * @author Mr.Sam
 */
import java.util.Hashtable;
import java.util.Enumeration;
import fr.ove.openmath.OpenMathizable;
/**
* This class to register the pair element name / value (it is a hashtable)
* and indicates if all the registrations have been done.
*/
public class CheckedRegistry  {
    private Hashtable hashtable;
    
    /**
    * The constructor.
    */
    public CheckedRegistry() {
        hashtable = new Hashtable();
    }
    
    /**
    * Adds an element in the registery.
    * @param id the identifier.
    * @param omizable the object to identify.
    * @param arg the argument number.
    */
    public void add(String id, OpenMathizable omizable, String arg) {
        
        System.out.println("On ajoute [" + id + ", "+ arg +"]");
        
        hashtable.put(id, new Association(arg, omizable));
    }
    
    /**
    * Removes the specified element form the registery.
    * @param id the elment to remove.
    */
    public void remove(String id) {
        hashtable.remove(id);
    }
    
    /**
    * Set the value of the specified element.
    * @param id the identifier.
    * @param omizable the object to identify.
    */
    public void setOMizable(String id, OpenMathizable omizable) {
        Association pair = (Association) hashtable.get(id);
        if (pair != null)
            pair.omizable = omizable;
    }
    
    
    /**
    * Returns <CODE>true</CODE> if each key has a non-null associated value.
    * <CODE>false</CODE> otherwise.
    */
    public boolean gotAll() {
        boolean gotAll = true;
        Association pair;
        for (Enumeration e = hashtable.elements(); e.hasMoreElements(); ) {
            pair = (Association) e.nextElement();
            if (pair.omizable == null)
                return false;
        }
        return gotAll;
    }
    
    /**
    * Returns the argument name of the element with the specified identifier.
    * @param id the spcified identifier.
    */
    public String getArg(String id) {
        Association pair = (Association) hashtable.get(id);
        
        if (pair != null)
            return pair.arg;
        else
            return null;
    }
    
    
    
    
    /**
    * Association of an identifier and the object it identifies.
    */
    private class Association {
        /**
        * The identifier.
        */
        public String arg;
        
        /***
        * The object to identify.
        */
        public OpenMathizable omizable;
        
        /**
        * The constructor.
        * @param arg the identifier.
        * @param the object to identify.
        */
        public Association(String arg, OpenMathizable omizable) {
            this.arg = arg;
            this.omizable = omizable;
        }
    }    
}